class Platform {
    static $websocket() {
        Object.defineProperty(window['$websocket'], 'areas', {
            set: function (data) {
                Room.renderRoom(data)
            }
        })
        Object.defineProperty(window['$websocket'], 'tables', {
            set: function (data) {
                Desk.renderDesk(data)
            }
        })
        Object.defineProperty(window['$websocket'], 'name', {
            set: function (data) {
                Platform.username.text = data
            }
        })
        Object.defineProperty(window['$websocket'], 'money', {
            set: function (data) {
                Platform.usermoney.text = data
            }
        })
        Object.defineProperty(window['$websocket'], 'isHallShow', {
            set: function (data) {
                data ? egret.Tween.get(Platform.deskArea).to({
                    alpha: 0
                }, 750) : egret.Tween.get(Platform.deskArea).to({
                    alpha: 1
                }, 750)
            }
        })
        // window['$websocket']['areas'] = [{ num: 6 }]
        // window['$websocket']['name'] = 'q1wrsdv'
        // window['$websocket']['money'] = 1111
    }
    static render() {
        Platform.addBg()
        Platform.addHeader()
        Platform.addFooter()
        Platform.addRoom()
        Platform.addDesk()
        Platform.addIndex()
    }
    static addBg() {
        let bg = Functions.createBitmapByName("bg_png")
        Main.self.addChild(bg)
        bg.width = 1334
        bg.height = 750
    }
    static onUserHeadComplete(event) {
        var texture = new egret.Texture;
        texture.bitmapData = event.target.data;
        let Bitmap = new egret.Bitmap;
        Bitmap.texture = texture
        Bitmap.width = Platform.header_.width
        Bitmap.height = Platform.header_.height
        Platform.header_.addChild(Bitmap)

        var frame = Functions.createBitmapByName("user_head_frame_png")
        frame.width = Platform.header_.width
        frame.height = Platform.header_.height
        Platform.header_.addChild(frame)

        var back = Functions.createBitmapByName("back_png")
        back.width = 64
        back.height = 56
        back.x = 7
        back.y = 15
        back.visible = false
        back.touchEnabled = true
        back.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            egret.Tween.get(Platform.header).to({
                y: -150,
                alpha: 0
            }, 500).to({
                y: 0,
                alpha: 1
            }, 500)
            egret.Tween.get(Platform.footer).to({
                y: 881,
                alpha: 0
            }, 500).to({
                y: 661,
                alpha: 1
            }, 500)
            egret.Tween.get(Platform.deskArea).to({
                alpha: 0
            }, 500).call(function () {
                Platform.header_.getChildAt(0).visible = true
                Platform.header_.getChildAt(1).visible = true
                Platform.header_.getChildAt(2).visible = false
                Platform.deskArea.visible = false
                Platform.index.visible = false
                Platform.roomArea.visible = true
                egret.Tween.get(Platform.roomArea).to({
                    alpha: 1
                }, 500)
            })
        }, null)
        Platform.header_.addChild(back)
    }

    static header = new egret.DisplayObjectContainer
    static header_ = new egret.DisplayObjectContainer
    static username = new egret.TextField
    static usermoney = new egret.TextField
    static addHeader() {
        Main.self.addChild(Platform.header);
        Platform.header.width = Config.platform_hall_header_width;
        Platform.header.height = Config.platform_hall_header_height;

        Platform.header.addChild(Platform.header_);
        Platform.header_.width = 85;
        Platform.header_.x = 23
        Platform.header_.height = 85;
        Platform.header_.y = 9

        var loader = new egret.ImageLoader;
        loader.addEventListener(egret.Event.COMPLETE, Platform.onUserHeadComplete, null);
        var url = "datas/men_head.png";
        loader.load(url);

        for (let name in Config.platform_hall_header_pictures) {
            let object = Functions.createBitmapByName(name + "_png")
            Platform.header.addChild(object)
            for (let attr in Config.platform_hall_header_pictures[name]) {
                object[attr] = Config.platform_hall_header_pictures[name][attr]
            }
            if (object.touchEnabled) object.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
                Functions.animateBeat(object)
            }, null)
        }

        Platform.header.addChild(Platform.username);
        Platform.username.fontFamily = 'Microsoft Yahei'
        for (let attr in Config.platform_hall_header_text.username) {
            Platform.username[attr] = Config.platform_hall_header_text.username[attr]
        }

        Platform.header.addChild(Platform.usermoney);
        Platform.usermoney.fontFamily = 'Microsoft Yahei'
        for (let attr in Config.platform_hall_header_text.usermoney) {
            Platform.usermoney[attr] = Config.platform_hall_header_text.usermoney[attr]
        }
    }
    static footer = new egret.DisplayObjectContainer;
    static addFooter() {
        Main.self.addChild(Platform.footer);
        Platform.footer.y = 661;
        Platform.footer.width = Config.platform_hall_footer_width;
        Platform.footer.height = Config.platform_hall_footer_height;

        for (let name in Config.platform_hall_footer_pictures) {
            let object = Functions.createBitmapByName(name + "_png")
            Platform.footer.addChild(object)
            for (let attr in Config.platform_hall_footer_pictures[name]) {
                object[attr] = Config.platform_hall_footer_pictures[name][attr]
            }
        }
    }
    static roomArea = new egret.DisplayObjectContainer();
    static addRoom() {
        Main.self.addChild(Platform.roomArea);
        Platform.roomArea.y = 156;
        Platform.roomArea.width = Config.platform_hall_roomArea_width;
        Platform.roomArea.height = Config.platform_hall_roomArea_height;
    }
    static deskArea = new eui.Group;
    static addDesk() {
        Main.self.addChild(Platform.deskArea);
        Platform.deskArea.visible = false;
        Platform.deskArea.alpha = 0;
    }
    static index = new egret.DisplayObjectContainer;
    static addIndex() {
        Main.self.addChild(Platform.index)
        Platform.index.width = 1334;
        Platform.index.height = 50;
        Platform.index.y = 595;
        Platform.index.x = 45;
        Platform.index.visible = false;
    }
    // static enterDeskFrame = 0;
    // static enterDesk() {
    //     Platform.enterDeskFrame++;
    //     // action 32 times 150/32=4.6875  1/32=0.03125
    //     if (Platform.enterDeskFrame <= 32) {
    //         Platform.header.y = 0 - Platform.enterDeskFrame * 4.6875
    //         Platform.header.alpha = 1 - Platform.enterDeskFrame * 0.03125
    //         Platform.footer.y = 661 + Platform.enterDeskFrame * 4.6875
    //         Platform.footer.alpha = 1 - Platform.enterDeskFrame * 0.03125
    //         Platform.roomArea.alpha = 1 - Platform.enterDeskFrame * 0.03125
    //     }
    //     if (Platform.enterDeskFrame > 32) {
    //         var num = Platform.enterDeskFrame - 32
    //         Platform.header.y = -150 + num * 4.6875
    //         Platform.header.alpha = 0 + num * 0.03125
    //         Platform.footer.y = 811 - num * 4.6875
    //         Platform.footer.alpha = 0 + num * 0.03125
    //         Platform.deskArea.alpha = 0 + num * 0.03125
    //     }
    //     if (Platform.enterDeskFrame === 32) {
    //         Platform.header_.getChildAt(0).visible = false
    //         Platform.header_.getChildAt(1).visible = false
    //         Platform.header_.getChildAt(2).visible = true
    //         Platform.deskArea.visible = true
    //         Platform.index.visible = true
    //         Platform.roomArea.visible = false
    //     }
    //     if (Platform.enterDeskFrame === 64) {
    //         Platform.enterDeskFrame = 0
    //         Main.self.removeEventListener(egret.Event.ENTER_FRAME, Platform.enterDesk, null)
    //     }

    // }
    // static enterRoomFrame = 0;
    // static enterRoom() {
    //     Platform.enterRoomFrame++;
    //     // action 32 times 150/32=4.6875  1/32=0.03125
    //     if (Platform.enterRoomFrame <= 32) {
    //         Platform.header.y = 0 - Platform.enterRoomFrame * 4.6875
    //         Platform.header.alpha = 1 - Platform.enterRoomFrame * 0.03125
    //         Platform.footer.y = 661 + Platform.enterRoomFrame * 4.6875
    //         Platform.footer.alpha = 1 - Platform.enterRoomFrame * 0.03125
    //         Platform.deskArea.alpha = 1 - Platform.enterRoomFrame * 0.03125
    //     }
    //     if (Platform.enterRoomFrame > 32) {
    //         var num = Platform.enterRoomFrame - 32
    //         Platform.header.y = -150 + num * 4.6875
    //         Platform.header.alpha = 0 + num * 0.03125
    //         Platform.footer.y = 811 - num * 4.6875
    //         Platform.footer.alpha = 0 + num * 0.03125
    //         Platform.roomArea.alpha = 0 + num * 0.03125
    //     }
    //     if (Platform.enterRoomFrame === 32) {
    //         Platform.header_.getChildAt(0).visible = true
    //         Platform.header_.getChildAt(1).visible = true
    //         Platform.header_.getChildAt(2).visible = false
    //         Platform.deskArea.visible = false
    //         Platform.index.visible = false
    //         Platform.roomArea.visible = true
    //     }
    //     if (Platform.enterRoomFrame === 64) {
    //         Platform.enterRoomFrame = 0
    //         Main.self.removeEventListener(egret.Event.ENTER_FRAME, Platform.enterRoom, null)
    //     }
    // }
}

class Room {
    static roomCeil;
    static roomContainerList;
    static renderRoom(data) {

        Room.roomContainerList = []
        Platform.roomArea.removeChildren()

        Room.roomCeil = Math.ceil(data.length / 4)
        for (let i = 0; i < Room.roomCeil; i++) {
            (function (i) {
                var container = new egret.DisplayObjectContainer
                Platform.roomArea.addChild(container);
                container.x = i * 1334
                container.width = Config.platform_hall_roomArea_width;
                container.height = Config.platform_hall_roomArea_height;
                for (let i = 1; i <= 4; i++) {
                    (function (i) {
                        var container_ = new egret.DisplayObjectContainer
                        container.addChild(container_);
                        container_.width = 496
                        container_.height = 203
                        container_.y = (i <= 2) ? 0 : 220
                        container_.x = (i % 2 === 0) ? 756 : 90
                        Room.roomContainerList.push(container_)
                    })(i)
                }
            })(i)
        }
        for (let i = 0; i < data.length; i++) {
            for (let name in Config.platform_hall_roomArea_pictures) {
                let object = Functions.createBitmapByName(name + "_png")
                Room.roomContainerList[i].addChild(object)
                for (let attr in Config.platform_hall_roomArea_pictures[name]) {
                    object[attr] = Config.platform_hall_roomArea_pictures[name][attr]
                }
                if (name === "LobbyGameRoomItem") {
                    object.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
                        // window['$websocket']['tables'] = [{ num: 1 }]
                        window['$api'].enterArea(i)
                    }, null)
                }
            }
            for (let name in Config.platform_hall_roomArea_text) {
                var text = new egret.TextField;
                Room.roomContainerList[i].addChild(text);
                for (let attr in Config.platform_hall_roomArea_text[name]) {
                    text[attr] = Config.platform_hall_roomArea_text[name][attr]
                }
                text.fontFamily = 'Microsoft Yahei'

                if (name === 'number') {
                    text.text = data[i].num
                } else {
                    text.text = 'example'
                }
            }
        }
        return true;
    }

}

class Desk {
    static deskContainerList;
    static deskCeil;
    static scroller;
    static xArr;
    static originX;
    static renderDesk(data) {
        egret.Tween.get(Platform.header).to({
            y: -150,
            alpha: 0
        }, 500).to({
            y: 0,
            alpha: 1
        }, 500)
        egret.Tween.get(Platform.footer).to({
            y: 881,
            alpha: 0
        }, 500).to({
            y: 661,
            alpha: 1
        }, 500)
        egret.Tween.get(Platform.roomArea).to({
            alpha: 0
        }, 500).call(function () {
            Platform.header_.getChildAt(0).visible = false
            Platform.header_.getChildAt(1).visible = false
            Platform.header_.getChildAt(2).visible = true
            Platform.deskArea.visible = true
            Platform.index.visible = true
            Platform.roomArea.visible = false
            egret.Tween.get(Platform.deskArea).to({
                alpha: 1
            }, 500)
        })

        Desk.xArr = []
        Desk.deskContainerList = []
        Platform.index.removeChildren()
        Platform.deskArea.removeChildren()

        Desk.deskCeil = Math.ceil(data.length / Config.platform_hall_desk_max_player)

        for (let i = 0; i < Desk.deskCeil; i++) {
            (function (i) {
                var container = new eui.Group
                Platform.deskArea.addChild(container);
                container.x = i * 1334
                container.width = 1334;
                container.height = Config.platform_hall_deskArea_height;
                for (let i = 1; i <= Config.platform_hall_desk_max_player; i++) {
                    (function (i) {
                        var container_ = new eui.Group
                        container.addChild(container_);
                        container_.width = 444
                        container_.height = 145
                        container_.y = (i <= 2) ? 0 : (i <= 4) ? 150 : 300
                        container_.x = (i % 2 === 0) ? 781 : 115
                        Desk.deskContainerList.push(container_)
                    })(i)
                }
            })(i)

            {
                let name_1 = "LobbyCente_Prompt_png"
                let name_2 = "page_Prompt_png"
                let gaps = Desk.deskCeil - 1
                let gapWidth = 50
                Desk.originX = (1334 - gaps * Config.platform_hall_deskCeil_index[name_2].width - Config.platform_hall_deskCeil_index[name_1].width - gaps * gapWidth) / 2;
                let o_;
                switch (i) {
                    case 0:
                        o_ = Functions.createBitmapByName(name_1)
                        Platform.index.addChild(o_);
                        for (let attr in Config.platform_hall_deskCeil_index[name_1]) {
                            o_[attr] = Config.platform_hall_deskCeil_index[name_1][attr]
                        }
                        o_.x = Desk.originX
                        break;
                    default:
                        o_ = Functions.createBitmapByName(name_2)
                        Platform.index.addChild(o_);
                        for (let attr in Config.platform_hall_deskCeil_index[name_2]) {
                            o_[attr] = Config.platform_hall_deskCeil_index[name_2][attr]
                        }
                        o_.x = Desk.originX + i * gapWidth
                        break;
                }
                Desk.xArr.push(o_.x)
            }


        }
        Desk.scroller = new eui.Scroller;
        Main.self.addChild(Desk.scroller);
        Desk.scroller.x = 0;
        Desk.scroller.y = 143;
        Desk.scroller.width = 1334;
        Desk.scroller.height = Config.platform_hall_deskArea_height;
        Desk.scroller.viewport = Platform.deskArea;
        Desk.scroller.throwSpeed = 0
        Desk.scroller.bounces = false
        Platform.deskArea.addEventListener(egret.TouchEvent.TOUCH_MOVE, Desk.scrollerTouchMove, null)
        Desk.scroller.addEventListener(eui.UIEvent.CHANGE_END, Desk.scrollerTouchOver, null)
        for (let i = 0; i < data.length; i++) {
            for (let name in Config.platform_hall_deskArea_pictures) {

                let object = Functions.createBitmapByName(name + "_png")
                Desk.deskContainerList[i].addChild(object)
                for (let attr in Config.platform_hall_deskArea_pictures[name]) {
                    object[attr] = Config.platform_hall_deskArea_pictures[name][attr]
                }
                if (name === "roomBoard") {
                    object.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
                        window['$api'].enterTable(i)
                    }, null)
                }

                var shpBeMask = new egret.Shape;
                Desk.deskContainerList[i].addChild(shpBeMask);
                (name === 'progress_bar') && (object.mask = shpBeMask);
                (name === 'progress_bar_') && (object.mask = shpBeMask);
                shpBeMask.graphics.beginFill(0xbe2620);
                shpBeMask.graphics.drawRect(
                    Config.platform_hall_deskArea_pictures.progress_bar.x,
                    Config.platform_hall_deskArea_pictures.progress_bar.y,
                    Config.platform_hall_deskArea_pictures.progress_bar.width / Config.platform_hall_desk_max_player * data[i]['num'],
                    Config.platform_hall_deskArea_pictures.progress_bar.height
                );
                shpBeMask.graphics.endFill();
                shpBeMask.visible = false;
                setTimeout(function () {
                    shpBeMask.visible = true;
                })
            }
            var init = 0
            for (let name in Config.platform_hall_deskArea_text) {
                var text = new egret.TextField();
                Desk.deskContainerList[i].addChild(text);
                for (let attr in Config.platform_hall_deskArea_text[name]) {
                    text[attr] = Config.platform_hall_deskArea_text[name][attr]
                }
                text.fontFamily = 'Microsoft Yahei'
                switch (init) {
                    case 0:
                        text.text = `${i + 1}号桌`;
                        break;
                    case 1:
                        text.text = `${data[i]['num']}人在玩`;
                        break;
                    case 2:
                        text.text = "无限制";
                        break;
                    case 3:
                        text.text = `${data[i]['num']} / ${Config.platform_hall_desk_max_player}`;
                        break;
                }
                init++
            }
        }

    }

    static initX = null;
    static lastX = 0;
    static scrollerTouchMove(e) {
        if (!Desk.noScroll) {
            if (Desk.initX === null) {
                Desk.initX = e.stageX;
            } else {
                Desk.lastX = e.stageX;
            }
        }
    }
    static page = 1;
    static noScroll = false;
    static scrollerTouchOver() {
        if (!Desk.noScroll && Desk.initX) {
            Desk.scroller.scrollPolicyH = eui.ScrollPolicy.OFF;
            Desk.noScroll = true
            let distance = Desk.lastX - Desk.initX
            if (Math.abs(distance) > 75) {
                if (distance < 0) {
                    if (Desk.page != Desk.deskCeil) {
                        let x = Platform.index.getChildAt(0).x
                        Platform.index.getChildAt(0).x = Desk.xArr[Desk.page]
                        Platform.index.getChildAt(Desk.page).x = x
                        Desk.page++
                    }
                } else {
                    if (Desk.page != 1) {
                        let x = Platform.index.getChildAt(0).x
                        Platform.index.getChildAt(0).x = Desk.xArr[Desk.page - 2]
                        Platform.index.getChildAt(Desk.page - 1).x = x
                        Desk.page--
                    }
                }
            }
            egret.Tween.get(Desk.scroller.viewport).to({
                scrollH: Desk.page * 1334 - 1334
            }, 250).call(function () {
                Desk.initX = null;
                Desk.noScroll = false
                Desk.scroller.scrollPolicyH = eui.ScrollPolicy.ON;
            })
        }
    }
}